package com.xm.xmapp.tool;

import android.util.Base64;

import com.xm.xmapp.http.XMURL;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by LDW on 2017/7/14.
 */
public class JavaTool {
    // 密钥

    private static byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0 };

    public static String encode(String plainText) {
        try {

            IvParameterSpec zeroIv = new IvParameterSpec(iv);
            SecretKeySpec key = new SecretKeySpec(
                    XMURL.secretKey.getBytes(), "DES");
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            byte[] encryptedData = cipher.doFinal(plainText.getBytes());
            return Base64.encodeToString(encryptedData, 1);
        } catch (Exception e) {
            return null;
        }
    }
}
